const { HabitUrl, IconCategory } = require("../../../config/config")

// pages/habit/addHabit/addHabit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentHabit:{
      name:'',
      iconUrl:'',
      uid:'',
      createTime:''   
    },
    selectHabitId:'',
    currentIndexNav:0,
    habitIcon : '../../../images/sun.png',
    habitName:'',
    iconCategory:[],
    habitCreateTime:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    if(options.selectHabitId){
      _this.setData({
        selectHabitId :options.selectHabitId
      })
      wx.request({
        url: HabitUrl+'/'+options.selectHabitId,
        method: 'GET',  
        success: function(res){
          _this.setData({
            habitIcon : res.data.result.iconUrl,
            habitName : res.data.result.name,
            habitCreateTime : res.data.result.createTime
          })
        }
      })
    }
    wx.request({
      url: IconCategory+'/list',
      method: 'GET', 
      success: function(res){
        _this.setData({
          iconCategory :res.data.result
        })
      }
    })
    
    
  },
  activeNav:function(e){
    this.setData({
      currentIndexNav: e.target.dataset.index
    })
  },
  contentChange:function(e){
    this.setData({
      currentIndexNav: e.detail.current
    })
  },
  changIcon:function(e){ 
    this.setData({
      habitIcon:e.currentTarget.dataset.icon
    })
  },
  saveHabit:function(){
    if(this.data.habitName == ''){
      wx.showToast({
        title: '习惯名不能为空',
        icon: 'none',
        duration: 1000
      });  
    }else{
      let _this = this
      let habitName = this.data.habitName
      let habitIcon = this.data.habitIcon
      if(_this.data.selectHabitId != '' && _this.data.selectHabitId > 9){
        //编辑习惯 带上hid 带上创建习惯时间
        //不能获取到习惯库的id
        _this.data.currentHabit['id'] = _this.data.selectHabitId
        _this.setData({
          'currentHabit.createTime':this.data.habitCreateTime
        })
      }else{
        //新增习惯,带上现在的时间
        // _this.setData({
        //  'currentHabit.createTime':new Date
        // })
      }
      _this.setData({
        'currentHabit.name' : habitName,
        'currentHabit.iconUrl': habitIcon,
        'currentHabit.uid':wx.getStorageSync('userinfo').id
      })
      wx.request({
        url: HabitUrl,
        data: _this.data.currentHabit,
        method: 'POST',
        success: function(res){
          if(res.data.code === 200){
            let message = res.data.result
            wx.showToast({
              title: message,
              icon: 'success',
              duration: 1000
            });  
            wx.switchTab({
              url: '/pages/habit/habitIndex/habitIndex'
            })
          }else{
            let message = res.data.message
            wx.showToast({
              title: message,
              icon: 'none',
              duration: 1000
            });  
          }
        }
      })
    }
  },
  bindHabitName:function(e){
    this.setData({
      habitName : e.detail.value
    })
  },
})